package com.ease.concurrent.thread.state;

/**
 * 3. Wait时处于什么状态？
 * @author: chenhs
 * @date: Created in 10:13 2019/12/27
 **/
public class ThreadStateDemo4_Wait {

    public static void main(String[] args) throws Throwable {
        Object obj = new Object();

        Thread t1 = new Thread(() -> {

            synchronized (obj) {
                try {
                    System.out.println(" t1将wait(3000L)");
                    obj.wait(3000L);
                    System.out.println(" t1将wait()");
                    obj.wait();
                    System.out.println(" t1执行完");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t1.start();

        Thread.sleep(1000L);

        synchronized (obj) {
            // timed_waiting
            System.out.println("t1的状态：" + t1.getState());
            obj.notify();
            Thread.sleep(1000L);
            /**t1只是被唤醒，此时锁在主线程手里，所以t1是BLOCKED状态*/
            // blocked
            System.out.println("t1的状态：" + t1.getState());
        }

        Thread.sleep(3000L);
        /**这时t1拿到锁了*/
        // waiting
        System.out.println("t1的状态：" + t1.getState());

        Thread.sleep(1000L);
        synchronized (obj) {
            obj.notify();
        }
        // runnable
        System.out.println("t1的状态x：" + t1.getState());
        Thread.sleep(1000L);
        // terminated
        System.out.println("t1的状态y：" + t1.getState());


        //什么时候线程让出锁？ sleep()
        //什么时候线程让出CPU？ wait()
    }
}
